/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package validators;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author Pablo
 */
@FacesValidator (value="costeValidator")
public class CosteValidator implements Validator{
    @Override
    public void validate(FacesContext context, UIComponent component, Object value) 
            throws ValidatorException {
        String validar = context.getExternalContext().getRequestParameterMap().get("validar");
        if (validar != null && validar.equals("true")) {
            //Short att = (Short) component.getAttributes().get("coste");
            //Integer attribute;
            /*
            if (att != null)
                attribute = new Integer (att);
            else 
                attribute = null;
                * */
            Double attribute = (Double) component.getAttributes().get("coste");
            if ((value == null && attribute != null) || (value != null && attribute == null)){
                FacesMessage message = new FacesMessage();
                message.setSummary("No se puede especificar sólo un límite de coste");
                message.setSeverity(FacesMessage.SEVERITY_ERROR);
                throw new ValidatorException(message);
            }        
            if ((value != null && attribute != null) && (attribute.compareTo((Double)value)>0)){
                FacesMessage message = new FacesMessage();
                message.setSummary("El coste de la izquierda debe ser menor que el de la derecha");
                message.setSeverity(FacesMessage.SEVERITY_ERROR);
                throw new ValidatorException(message);
            }
        }
    }
}
